package com.jt.config;

import com.jt.service.FileService;
import com.jt.vo.ImageVO;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;
    @PostMapping("/upload")
     public SysResult upload(MultipartFile file) throws IOException {
        ImageVO imageVO= fileService.upload(file);
        if (imageVO ==null){
            return SysResult.fail();
    }
        return SysResult.success(imageVO);
    }
//    高级API：MultipartFile 自动维护了缓存流/自动开关
//    @PostMapping("/upload")
//     public SysResult upload(MultipartFile file) throws IOException {
//     String filename=file.getOriginalFilename();
//     String fileDir="D:/images/";
//     File dir=new File(fileDir);
//     if (!dir.exists()){
//         dir.mkdirs();
//     }
//     String localPath=fileDir+filename;
//     file.transferTo(new File(localPath));
//        System.out.println("文件上传成功");
//        return SysResult.success();
//    }


}
